package cn.stronglink.asset.module.common.controller;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.util.PropertiesUtil;
import cn.stronglink.suite.core.util.UploadEntity;

@Controller
@RequestMapping(value = "upload")
public class UploadController extends AbstractController {
	
	@OperateLog(module = "资产型号",desc="上传资产型号图片", type = OpType.ADD)
	@RequestMapping(value = "uploadImg")
	public ResponseEntity<ModelMap> upload(@RequestParam(value = "file", required = true) MultipartFile file,HttpServletRequest request, HttpServletResponse response,
				ModelMap modelMap) {	
			if(!file.isEmpty()){
				try {  
					String suffix = file.getOriginalFilename().substring  
							(file.getOriginalFilename().lastIndexOf("."));
					String fid = UUID.randomUUID().toString().replace("-", "");
	                // 文件保存路径  
	                String filePath = PropertiesUtil.getString("upload_dir")
	                        + "/" + fid + suffix;  
	                if(filePath !=null){
	        			File ioFile = new File(filePath);
	        			if(!ioFile.isDirectory()){
	        				ioFile.mkdirs();
	        			}
	        		}

	                // 转存文件  
	                file.transferTo(new File(filePath));  
	                UploadEntity result = new UploadEntity();
	    			result.setFid(fid + suffix);
	    			result.setName(file.getOriginalFilename());
	    			result.setUrl(filePath);
	    			result.setSuccess(true);
	    			return setSuccessModelMap(modelMap, result);
	            } catch (Exception e) {  
	                e.printStackTrace();  
	            }  		
			}
			return setSuccessModelMap(modelMap, null);
		}
	
	
	@OperateLog(module = "上架单管理",desc="上传上架单申请附件", type = OpType.ADD)
	@RequestMapping(value = "uploadShelfUpFile")
	public ResponseEntity<ModelMap> uploadShelfUpFile(@RequestParam(value = "file", required = true) MultipartFile file,HttpServletRequest request, HttpServletResponse response,
				ModelMap modelMap) {	
			if(!file.isEmpty()){
				try {  
					DateFormat df= new SimpleDateFormat("yyyyMM");
			        String dirs=df.format(new Date());
					String suffix = file.getOriginalFilename().substring  
							(file.getOriginalFilename().lastIndexOf("."));
					String fid = UUID.randomUUID().toString().replace("-", "");
	                // 文件保存路径  
	                String filePath = PropertiesUtil.getString("upload_dir")
	                        + "/"+dirs+"/" + fid + suffix;  
	                if(filePath !=null){
	        			File ioFile = new File(filePath);
	        			if(!ioFile.isDirectory()){
	        				ioFile.mkdirs();
	        			}
	        		}

	                // 转存文件  
	                file.transferTo(new File(filePath));  
	                UploadEntity result = new UploadEntity();
	    			result.setFid(dirs+"/"+fid + suffix);
	    			result.setName(file.getOriginalFilename());
	    			result.setUrl(filePath);
	    			result.setSuccess(true);
	    			return setSuccessModelMap(modelMap, result);
	            } catch (Exception e) {  
	                e.printStackTrace();  
	            }  		
			}
			return setSuccessModelMap(modelMap, null);
		}
}
